<!-- from 7 5 2 7 1 8 9 2 0 -->
<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('reportQuery.productInformation')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input :placeholder="$t('reportQuery.barcodeModel')" v-model="queryParam.materialParam"></a-input>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('commodityManagement.category')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-tree-select style="width:100%" :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
                  :filterTreeNode="filterTreeNode"
                  showSearch
                    allow-clear :treeData="categoryTree" v-model="queryParam.categoryId"
                    :placeholder="$t('commodityManagement.pleaseCategory')">
                  </a-tree-select>
                </a-form-item>
              </a-col>
              
              <a-col :md="6" :sm="24">
                  <a-form-item :label="$t('basicInformation.warehouseName')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                    <a-select style="width: 200px" :placeholder="$t('basicInformation.warehouseName')" 
                    v-model="queryParam.depotId" allowClear >
                        <a-select-opt-group>
                          <span slot="label">{{$t('inPrint.self')}}</span>
                          <a-select-option v-for="(item, index) in selfDepot" :key="index" :value="item.id">
                          {{ item.depotName || item.name }}
                        </a-select-option>
                        </a-select-opt-group>
                        <a-select-opt-group :label="$t('inPrint.other')">
                          <a-select-option v-for="(item, index) in othoerDepot" :key="index" :value="item.id">
                          {{ item.depotName || item.name }}
                        </a-select-option>
                        </a-select-opt-group>
                      </a-select>
                    </a-form-item>
                </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('reportQuery.documentDate')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-range-picker
                    style="width: 100%"
                    v-model="queryParam.createTimeRange"
                    :default-value="defaultTimeStr"
                    format="YYYY-MM-DD"
                    :placeholder="[$t('reportQuery.startTime'), $t('reportQuery.endTime')]"
                    @change="onDateChange"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons  table-operator">
                  <a-button type="primary" @click="searchQuery">{{$t('reportQuery.query')}}</a-button>
                  <ColumnsSetting style="ma" @columnsChange="columnsChange" :columns="columns"></ColumnsSetting>
                  <a-button style="margin-left: 8px" @click="print" icon="printer">{{$t('reportQuery.print')}}</a-button>
                  <a-button v-if="btnEnableList.indexOf(5) > -1"  style="margin-left: 8px" @click="generatePrintData" icon="printer">{{$t('inPrint.other') + $t('reportQuery.print')}}</a-button>
                  <a-button v-if="btnEnableList.indexOf(3) > -1" :loading="downLoading" style="margin-left: 8px" @click="exportExcel" icon="download">{{$t('reportQuery.export')}}</a-button>
                </span>
              </a-col>
              <a-col :md="24" :sm="24">
                <a-form-item>
                  {{$t('reportQuery.note')}}：{{$t('reportQuery.thisReportContainsRetailAndSalesData')}}
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- table区域-begin -->
        <section ref="print" id="reportPrint">
          <a-table
            bordered
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            @change="handleTableChange">
          </a-table>
          <a-row :gutter="24" style="margin-top: 8px;text-align:right;">
            <a-col :md="24" :sm="24">
              <a-pagination @change="paginationChange" @showSizeChange="paginationShowSizeChange"
                size="small"
                show-size-changer
                :showQuickJumper="true"
                :current="ipagination.current"
                :page-size="ipagination.pageSize"
                :page-size-options="ipagination.pageSizeOptions"
                :total="ipagination.total"
                :show-total="(total, range) => $t('reportQuery.common') +` ${total-Math.ceil(total/ipagination.pageSize)} `+ $t('reportQuery.strip')">
                <template slot="buildOptionText" slot-scope="props">
                  <span>{{ props.value-1 }}{{$t('reportQuery.strip')}}/{{$t('reportQuery.page')}}</span>
                </template>
              </a-pagination>
            </a-col>
          </a-row>
        </section>
        <!-- table区域-end -->
      </a-card>
    </a-col>
    <saleOutRepotModual ref="saleOutRepotModual" :list="dataSource" :columns="columns" :queryParam="queryParam"></saleOutRepotModual>
  </a-row>
</template>
<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { getNowFormatYear, getMpListShort, openDownloadDialog, sheet2blob} from "@/utils/util"
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import moment from 'moment'
  import { queryMaterialCategoryTreeList} from '@/api/api'
  import {getAction} from '@/api/manage'
  import Vue from 'vue'
  import saleOutRepotModual from './modules/saleOutRepotModual';
  export default {
    name: "SaleOutReport",
    mixins:[JeecgListMixin],
    components: {
      JEllipsis,
      saleOutRepotModual
    },
    data () {
      return {
        labelCol: {
          span: 5
        },
        wrapperCol: {
          span: 18,
          offset: 1
        },
        queryParam: {
          categoryId:'',
          depotId:'',
          materialParam:'',
          beginTime: getNowFormatYear() + '-01-01',
          endTime: moment().format('YYYY-MM-DD'),
          mpList: getMpListShort(Vue.ls.get('materialPropertyList'))
        },
        ipagination:{
          pageSize: 11,
          pageSizeOptions: ['11', '21', '31', '101', '201']
        },
        dateFormat: 'YYYY-MM-DD',
        currentDay: moment().format('YYYY-MM-DD'),
        defaultTimeStr: '',
        tabKey: "1",
        selfDepot:[],
        othoerDepot:[],
        anotherDepotList:[],
        categoryTree:[],
        // 表头
        columns: [
          {
            title: this.$t('defaultTranslate.lineNumber'), dataIndex: 'rowIndex', width:60, align:"center",
            customRender:function (t,r,index) {
              return (t !== '合计') ? (parseInt(index) + 1) : t
            }
          },
          {title:this.$t('reportQuery.barcode'), dataIndex: 'barCode', width: 140},
          {title:this.$t('reportQuery.name'), dataIndex: 'materialName', width: 140},
          {title:this.$t('reportQuery.specification'), dataIndex: 'materialStandard', width: 120, ellipsis:true},
          { title: this.$t('reportQuery.category'), dataIndex: 'categoryName',width:80 },
          {title:this.$t('reportQuery.model'), dataIndex: 'materialModel', width: 80, ellipsis:true},
          // {title:this.$t('reportQuery.extendedInformation'), dataIndex: 'materialOther', width: 150, ellipsis:true},
          {title:this.$t('reportQuery.unit'), dataIndex: 'materialUnit', width: 80, ellipsis:true},
          {title:this.$t('commodityManagement.retailPrice'), dataIndex: 'commodityDecimal', width: 80, ellipsis:true},
          {title:this.$t('commodityManagement.SellingPrice'), dataIndex: 'wholesaleDecimal', width: 80, ellipsis:true},
          {title:this.$t('reportQuery.color'), dataIndex: 'materialColor', width: 80},
          {title:this.$t('basicInformation.warehouseName'), dataIndex: 'depotName', width: 80, ellipsis:true},
          {title:this.$t('reportQuery.salesQuantity'), dataIndex: 'outSum', sorter: (a, b) => a.outSum - b.outSum, width: 90},
          {title:this.$t('reportQuery.salesAmount'), dataIndex: 'outSumPrice', sorter: (a, b) => a.outSumPrice - b.outSumPrice, width: 90},
          {title:this.$t('reportQuery.purchasingCost'), dataIndex: 'cost', sorter: (a, b) => a.cost - b.cost, width: 90},
          {title:this.$t('reportQuery.purchasingCostAmount'), dataIndex: 'costAmount', sorter: (a, b) => a.costAmount - b.costAmount, width: 90},
          {title:this.$t('reportQuery.returnQuantity'), dataIndex: 'inSum', sorter: (a, b) => a.inSum - b.inSum, width: 90},
          {title:this.$t('reportQuery.returnAmount'), dataIndex: 'inSumPrice', sorter: (a, b) => a.inSumPrice - b.inSumPrice, width: 90},
          {title:this.$t('reportQuery.actualSalesAmount'), dataIndex: 'outInSumPrice', sorter: (a, b) => a.outInSumPrice - b.outInSumPrice, width: 100}
        ],
        url: {
          list: "/depotItem/saleOut"
        }
      }
    },
    created () {
      this.defaultTimeStr = [moment(getNowFormatYear() + '-01-01', this.dateFormat), moment(this.currentDay, this.dateFormat)]
      this.loadTreeData()
      this.getAnotherDepotData()
    },
    methods: {
      moment,
      create(){
      },
      getQueryParams() {
        let param = Object.assign({}, this.queryParam, this.isorter);
        param.monthTime = this.queryParam.monthTime;
        param.field = this.getQueryField();
        param.currentPage = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize-1;
        return param;
      },
      onDateChange: function (value, dateString) {
        console.log(dateString[0],dateString[1]);
        this.queryParam.beginTime=dateString[0];
        this.queryParam.endTime=dateString[1];
      },
      searchQuery() {
        if(this.queryParam.beginTime == '' || this.queryParam.endTime == ''){
          this.$message.warning(this.$t('reportQuery.pleaseDate'))
        } else {
          this.loadData(1);
        }
      },
      
       
    filterTreeNode(input,treeNode){
        return treeNode.data.props.title.toLowerCase().includes(input.toLowerCase())
      }, 
    

      getLocalStorage (key) {
      var value = localStorage.getItem(key) // 取回 value 变量
      this.localStorageKeyJson = JSON.parse(value)
      return this.localStorageKeyJson.value
    },
      getAnotherDepotData() {
        getAction('/depot/findDepotList').then(res => {
          if (res.code === 200) {
            this.anotherDepotList = res.data
            let creator = this.getLocalStorage('pro__Login_Userinfo').tenantId
            this.selfDepot = this.anotherDepotList.filter(v=>v.tenantId === creator)
            this.othoerDepot = this.anotherDepotList.filter(v=>v.tenantId !== creator)
          } else {
            this.$message.info(res.data)
          }
        })
    },
    
    
    loadTreeData() {
      let that = this;
      let params = {};
      params.id = '';
      queryMaterialCategoryTreeList(params).then((res) => {
        if (res) {
          that.categoryTree = [];
          for (let i = 0; i < res.length; i++) {
            let temp = res[i];
            that.categoryTree.push(temp);
          }
        }
      })
    },
    print(){
      this.$refs.saleOutRepotModual.show()
    }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>